package cn.schoolwow.quickdao.module.database.dml.instance.flow.update.flow.updateType;

import cn.schoolwow.quickdao.domain.entity.Entity;
import cn.schoolwow.quickdao.domain.entity.Property;
import cn.schoolwow.quickflow.domain.FlowContext;
import cn.schoolwow.quickflow.flow.BusinessFlow;

import java.util.ArrayList;
import java.util.List;

public class SetPropertyListByIdFlow implements BusinessFlow {
    @Override
    public void executeBusinessFlow(FlowContext flowContext) throws Exception {
        setSetPropertyList(flowContext);
        setWherePropertyList(flowContext);
    }

    @Override
    public String name() {
        return "更新实例-设置set和where字段列表-根据实例id字段";
    }

    private void setSetPropertyList(FlowContext flowContext){
        Entity entity = flowContext.checkData("entity", Entity.class);

        if(!flowContext.containKey("setPropertyList")){
            List<Property> setPropertyList = new ArrayList<>();
            for(Property property:entity.properties){
                if (property.id) {
                    continue;
                }
                if (property.createdAt) {
                    continue;
                }
                setPropertyList.add(property);
            }
            flowContext.putTemporaryData("setPropertyList", setPropertyList);
        }
    }

    private void setWherePropertyList(FlowContext flowContext){
        Entity entity = flowContext.checkData("entity", Entity.class);

        List<Property> wherePropertyList = new ArrayList<>();
        wherePropertyList.add(entity.id);
        flowContext.putTemporaryData("wherePropertyList", wherePropertyList);
    }
}
